from getopt import getopt


class OptMap:
    """
    OptMap is a wrapper to optlist returned by getopt.
    """
    
    def __init__(self, *args, **keywords):
        optlist, self._args = getopt(*args);

        verbose = keywords.has_key('verbose') and keywords['verbose'] 
        if verbose:
            print "input args = ", args
            print "parsed optlist = ", optlist
            print "output args = ", self._args
            
        self._optmap = {}
        for opt in optlist:
            self._optmap[opt[0]] = opt[1]

    def hasOpt(self, opt):
        return self._optmap.has_key(opt)

    def getOptValue(self, opt):
        if self._optmap.has_key(opt):
            return self._optmap[opt]
        else:
            return None

    def opt_length(self):
        return len(self._optmap)

    def length(self):
        return len(self._args)

    def getOpts(self):
        return [(k, v) for k, v in self._optmap] 

    def getArgs(self):
        return self._args

    def getArg(self, index):
        return self._args[index]
